/**
 * 程序访问入口
 *
 * */
//====================引入模块=================
const   express = require('express'),
        path = require('path'),
        logger = require('morgan'),
        bodyParser = require('body-parser'),
        cookieParser = require('cookie-parser'),
        compression = require('compression'),
        mongoose = require('mongoose'),
        rootConfig=require('./config/rootConfig'),

//====================引入路由=================
        user = require('./routes/user'),
        role = require('./routes/role'),
        auth = require('./routes/auth'),
        validate = require('./routes/validate'),
        index=require('./routes/index'),
        levelOneClient=require('./routes/levelOneClient')
        app = express();
mongoose.set('useCreateIndex', true)  //要修复弃用警告
mongoose.connect(rootConfig.mongooseDbUrl,{ useNewUrlParser: true ,useUnifiedTopology:true});//链接数据库
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(compression({level: 9}));//优化压缩静态资源
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

/*  前台路由   end  */
app.use('/',index);
app.use('/',validate); //验证token是否超时

app.use('/auth',auth);   //权限相关的接口
app.use('/levelOneClient',levelOneClient);   //用户相关的接口
app.use('/users',user);   //用户相关的接口
app.use('/role',role);   //角色相关的接口








// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.send(err)
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.send(err)
});



module.exports = app;
